﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_Logger.h".
//-------------------------------------------------------------------

#ifndef _UXS_STD_CONSOLE_OUTPUT_H
#define _UXS_STD_CONSOLE_OUTPUT_H

#pragma once

#include "ILogger.h"
#include "stdcout/StdConsole.h"

namespace UX_STL
{
namespace UXLogger
{

	//
	// Std Console
	//

	class CStdConsoleOutput : public ILogOutput
	{
	private:
		Thread		_sThread;
		bool		_bLooping	: 1;

		static void _s_ThreadProc(void *);

	public:
		bool Create();

		// ILogOutput //
		virtual void Write(const char *pMsg, e_log_msg::type eType, const char *pSrcFile, uint uLine);
		virtual void NewLine();
		virtual void Free();
		virtual e_log_output::bits	GetTypeBits() const		{ return e_log_output::CONSOLE_BIT; }
	};
	



	inline bool CStdConsoleOutput::Create()
	{
		_bLooping = true;
		return _sThread.Create( &_s_ThreadProc, this );
	}
	

	inline void CStdConsoleOutput::_s_ThreadProc(void *pThis)
	{
		if ( !_logger_hidden_::CreateConsole() )
			return;

		CStdConsoleOutput *	pthis = (CStdConsoleOutput *)pThis;

		while ( pthis->_bLooping )
		{
		};
		return;
	}


	inline void CStdConsoleOutput::Write(const char *pMsg, e_log_msg::type eType, const char *pSrcFile, uint uLine)
	{
		string	str;

		switch ( eType )
		{
			case e_log_msg::_LMP_INFO	 :
			case e_log_msg::_LMP_NONE	 :		break;
			case e_log_msg::_LMP_WARNING :		str << "WARNING: ";	break;
			case e_log_msg::_LMP_ERROR	 :		str << "ERROR: ";	break;
			case e_log_msg::_LMP_FATAL	 :		str << "FATAL: ";	break;
			case e_log_msg::_LMP_DEBUG	 :		str << "DEBUG: ";	break;
			case e_log_msg::_LMP_SYSTEM	 :		str << "SYSTEM: ";	break;
			case e_log_msg::_LMP_USER	 :		str << "USER: ";	break;
		}

		str << pMsg;
		
		if ( EnumCmp( eType, e_log_msg::_LMPX_FILE_LINE ) and pSrcFile != null and uLine != -1 )
			str << "( file: \"" << pSrcFile << "\", line: " << uLine << ')';

		_logger_hidden_::WriteToConsole( str.cstr() );
	}


	inline void CStdConsoleOutput::NewLine()
	{
		_logger_hidden_::ConsoleNewLine();
	}


	inline void CStdConsoleOutput::Free()
	{
		_bLooping = false;
		delete this;
	}

//-------------------------------------------------------------------

}	// UXLogger
}	// UX_STL

#endif	// _UXS_STD_CONSOLE_OUTPUT_H